package com.yunupmall.uits;

import cn.hutool.core.util.ObjectUtil;
import com.yunupmall.entity.ImageData;
import com.yunupmall.entity.admin.commoditypojo.TextImageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;

@RestController
public class ImagerContronller {
    @Autowired
    private Result result;
    @RequestMapping("/imagefile")
    public Result imagefile(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            String upload = Filepicture.upload(file);
            if (ObjectUtil.isNotEmpty(upload)){
                return result.ok(upload);
            }else{
                return result.error("上传失败");
            }
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }


    @RequestMapping("/Textimagefile")
    public TextImageData Textimagefile(@RequestParam("image") MultipartFile file) throws IOException {
        String upload = Filepicture.Textupload(file);
        TextImageData.DataDTO dataDTO = new TextImageData.DataDTO(upload);
        TextImageData.DataDTO[] dataDTOArray = new TextImageData.DataDTO[1];
        dataDTOArray[0] = dataDTO;
        TextImageData textImageData = new TextImageData();
        if (upload != null) {
            textImageData.setErrno(0);
            textImageData.setData(dataDTOArray);
        } else {
            textImageData.setErrno(1);
        }
        System.out.println(textImageData);
        return textImageData;
    }
}
